﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

using UploadManager;
//using StoreClientModule;
using UnityCore;

namespace MBB_Store_Sales_Report
{
    public partial class frmExpensesList : Form
    {
        
        int pp = 1;
        int cnt = 0;

        SynchronizeData sync = new SynchronizeData();
        Threader t = new Threader();
        readDetails path = new readDetails();

        public frmExpensesList()
        {
            InitializeComponent();
           
            btnView.Click += new EventHandler(btnView_Click);
            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);

            
            btnPrint.Click += new EventHandler(btnPrint_Click);

            sync.ProcessCompleted += new UnityCore.CoreHandler(sync_ProcessCompleted);
            sync.ProcessError += new UnityCore.CoreHandler(sync_ProcessError);
        }

        void sync_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(msg);
            });
        }

        void sync_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "GetStores":
                        cmbStore.Items.AddRange(sync.stores);
                        break;
                    case "Read":
                        DataTable dt = new DataTable();
                        dt = sync.export;
                        if (dt.Rows.Count > 0)
                        {
                            grd.DataSource = sync.export.DefaultView.ToTable(true, "fExpensesName", "fExpensesAmount", "fExpensesType");
                            grd.Columns[0].HeaderText = "Description";
                            grd.Columns[1].HeaderText = "Amount";
                            grd.Columns[2].HeaderText = "Type";

                            grd.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                            computeTotalExpenses();
                        }
                        else
                        {
                            MessageBox.Show("No Records Found", "Display Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        break;
                }

            });
        }

        void computeTotalExpenses()
        {
            double totalE = 0.0;
            for (int i = 0; i < grd.Rows.Count; i++)
            {
                double amtE = double.Parse(grd.Rows[i].Cells[1].Value.ToString());
                totalE += amtE;
            }
            txtTotal.Text = Common.FormatCurrency(totalE);
        }


        void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                //Tracking.EnterLog("Price Table is printed.", DateTime.Now.ToString());
                ppdlg.ShowDialog();
            }
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0, y2=0, tmp = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.MarginBounds.Left;
            float rm = e.PageBounds.Width - 100;
            float bm = e.PageBounds.Height - 100;
            float ht = 0;
            Font cFont;
            StringFormat sf = new StringFormat();
            float tmpStx = 0;
            float totalA = 0.0f, totalB = 0.0f;
            //Header
            y = 150;
            y2 = 150;

            cFont = new Font("Courier New", 10f, FontStyle.Regular);
            ht = cFont.GetHeight(e.Graphics);
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString(cmbStore.Text, new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2, 50, sf);
            e.Graphics.DrawString("", new Font("Courier New", 15f, FontStyle.Bold), Brushes.Black, e.PageBounds.Width / 2, 70, sf);

            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("Expenses ", cFont, Brushes.Black, lm, y, sf);
            sf.Alignment = StringAlignment.Far;
            e.Graphics.DrawString(DateTime.Now.ToShortDateString(), cFont, Brushes.Black, rm, y, sf);
            //Column Header
            y += ht;
            y2 += ht;

            e.Graphics.FillRectangle(Brushes.LightGray, lm, y, rm - lm, ht);
            e.Graphics.DrawRectangle(new Pen(Brushes.Black), lm, y, rm - lm, ht);
            

            sf.Alignment = StringAlignment.Center;
            float cx = lm + (((rm - lm) * 30) / 100);
            float tx = lm + (((rm - lm) * 15f) / 100);
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Expenses A", cFont, Brushes.Black, tx, y, sf);
            
            cx += ((rm - lm) * 20) / 100;
            tx = cx - ((rm - lm) * 20) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Amount", cFont, Brushes.Black, tx, y, sf);

            cx += ((rm - lm) * 30) / 100;
            tx = cx - ((rm - lm) * 30) / 200;
            e.Graphics.DrawLine(new Pen(Brushes.Black), cx, y, cx, y + ht);
            e.Graphics.DrawString("Expenses B", cFont, Brushes.Black, tx, y, sf);

            tx += ((rm - lm) * 20) / 100;
            e.Graphics.DrawString("Amount", cFont, Brushes.Black, tx + 25, y, sf);
            
            //Data
            
            y += ht * (1.5f);
            y2 += ht * (1.5f);
            ht = cFont.GetHeight(e.Graphics);
            lpp = (bm - y) / ht;
            int cl = 0;
            float stx = 0, stx2 = 0; 
            
            while (cl <= lpp && cnt < grd.Rows.Count)
            {
                sf.Alignment = StringAlignment.Near;
                stx = lm + 1;
                stx2 = lm + ((rm - lm) * 51) / 100;

                if (grd.Rows[cnt].Cells[2].Value.ToString().Equals("A"))
                {
                    string str = grd.Rows[cnt].Cells[0].Value.ToString();
                    if (str.Length > 23)
                        str = str.Substring(0, 20) + "...";
                    e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);

                    sf.Alignment = StringAlignment.Far;
                    stx += ((rm - lm) * 49) / 100;
                    str = grd.Rows[cnt].Cells[1].Value.ToString();
                    e.Graphics.DrawString(str, cFont, Brushes.Black, stx, y, sf);
                    y += ht;

                    totalA += float.Parse(str);
                }
                else
                {
                    string str = grd.Rows[cnt].Cells[0].Value.ToString();
                    if (str.Length > 23)
                        str = str.Substring(0, 20) + "...";
                    e.Graphics.DrawString(str, cFont, Brushes.Black, stx2, y2, sf);

                    sf.Alignment = StringAlignment.Far;
                    stx2 += ((rm - lm) * 49) / 100;
                    str = grd.Rows[cnt].Cells[1].Value.ToString();
                    e.Graphics.DrawString(str, cFont, Brushes.Black, stx2, y2, sf);
                    y2 += ht;

                    totalB += float.Parse(str);
                }
                
                cnt++;
                cl++;
            }
            
            if (y2 >= y)
            {
                tmp = y2;
                tmpStx = stx2;
            }
            else
            {
                tmp = y;
                tmpStx = stx;
            }
            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("Total", cFont, Brushes.Blue, lm + 1, tmp, sf);
            sf.Alignment = StringAlignment.Far;
            tmpStx = lm + ((rm - lm) * 49) / 100;
            e.Graphics.DrawString(Common.FormatCurrency(totalA), cFont, Brushes.Blue, tmpStx, tmp, sf);
            
            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawString("Total", cFont, Brushes.Blue, lm + 1+ ((rm - lm) * 51) / 100, tmp, sf);
            sf.Alignment = StringAlignment.Far;
            tmpStx += ((rm - lm) * 50) / 100;
            e.Graphics.DrawString(Common.FormatCurrency(totalB), cFont, Brushes.Blue, tmpStx+2, tmp, sf);
            
            if (cnt < grd.Rows.Count)
            {
                e.HasMorePages = true;
                pp++;
            }
            else
            {
                e.HasMorePages = false;
                cnt = 1;
                pp = 1;
            }
              
        }

        void btnView_Click(object sender, EventArgs e)
        {
            if (txtDate.Text != "" && cmbStore.Text != "")
            {
                path.date = txtDate.Text;
                path.remNode = cmbStore.Text;
                path.fileName = "Expenses";
                sync.Read(path);
            }
        }


        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlExpensesListOnline.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlExpensesListOnline; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            sync.Stores();
        }
    }
}
